package com.szbber.door.api

import com.google.gson.Gson
import com.szbber.door.utils.CommonUtil
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.*


class Api(okHttpClient: OkHttpClient) {

    init {
        val retrofit: Retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(Gson()))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .client(okHttpClient)
            .baseUrl(CommonUtil.getBaseUrl())
            .build()
        service =
            retrofit.create(ApiService::class.java)
    }

    companion object {
        private var instance: Api? = null
        private var service: ApiService? = null
        fun getInstance(): Api? {
            synchronized(Api::class.java) {
                if (instance == null) {
                    instance =
                        Okhttp.provideOkHttpClient()?.let { Api(it) }
                }
                return instance
            }
        }

        fun getMap(
            vararg s: String
        ): Map<String, String>? {
            val map: MutableMap<String, String> =
                HashMap()
            if (s.size % 2 != 0) {
                return null
            } else {
                var i = 0
                while (i < s.size) {
                    map[s[i]] = s[++i]
                    i++
                }
            }
            return map
        }
    }

    //连接测试
//    fun connectTest(): rx.Observable<ConnectTestBean?>? {
//        return service?.connectTest()
//    }



}